package commands;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import jesss.commands.JesssCommand;
import jesss.commands.JesssCommandFactoryImpl;
import jesss.commands.CommandResult;
import jesss.commands.ResultStatus;
import jesss.options.JesssOptions;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

/**
 * User: Lee
 * Date: 8/24/13
 * Time: 12:15 PM
 */
public class GetObjectCommandTests extends CommandTestsBase {

    private AmazonS3 mockS3 = mock(AmazonS3.class);
    private JesssOptions mockJesssOptions = mock(JesssOptions.class);

    @Before
    public void Setup() {
        stub(mockJesssOptions.getAmazonS3()).toReturn(mockS3);
        stub(mockS3.getObject(any(GetObjectRequest.class), any(java.io.File.class))).toReturn(makeTestObjectMetadata());
        stub(mockJesssOptions.getCommandName()).toReturn("go");
        stub(mockJesssOptions.getShouldDisplayVerbose()).toReturn(true);
    }

    @Test
    public void CanGetObject() throws Exception {
        String[] args = {"-bn", "testBucket", "-k", "key", "-vid", "versiondId", "-tf", "c:\\temp\\test2.txt"};
        JesssCommand getObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        CommandResult<ObjectMetadata> commandResult = getObjectCommand.executeCommand();
        if (commandResult.getResultStatus() == ResultStatus.Success) {
            ObjectMetadata objectMetadata = commandResult.getResultValue();
            Assert.assertNotNull("Returned ObjectMetadata was null, but should not be null.", objectMetadata);
        }
        verify(mockS3, times(1)).getObject(any(GetObjectRequest.class), any(java.io.File.class));
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingBucketParam() throws Exception {
        String[] args = {"-k", "testPrefix", "-tf", "c:\\temp\\test2.txt"};
        JesssCommand getObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        getObjectCommand.executeCommand();
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingTargetFileParam() throws Exception {
        String[] args = {"-bn", "testBucket", "-k", "key"};
        JesssCommand getObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        getObjectCommand.executeCommand();
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingKeyParam() throws Exception {
        String[] args = {"-bn", "testBucket", "-tf", "c:\\temp\\test2.txt"};
        JesssCommand getObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        getObjectCommand.executeCommand();
    }

}
